import { useEffect, useRef } from 'react';
export function useAutoScroll(_ref2) {
  var table = _ref2.table,
    scrollbar = _ref2.scrollbar;
  var _ref = useRef({
    raf: null,
    iT: null
  });
  var autoScroll = table.props.autoScroll;
  useEffect(function () {
    var container = table.outRef.current._outerRef;
    if (autoScroll && scrollbar.vertical) {
      var rIc = window.requestIdleCallback || window.setTimeout;
      var _ref3 = [function () {
          if (_ref.current.iT) {
            clearTimeout(_ref.current.iT);
            _ref.current.iT = null;
          }
          if (_ref.current.raf) {
            cancelAnimationFrame(_ref.current.raf);
            _ref.current.raf = null;
          }
        }, function () {
          _ref.current.raf = requestAnimationFrame(function () {
            rIc(function () {
              var _table$outRef$current;
              (_table$outRef$current = table.outRef.current) === null || _table$outRef$current === void 0 || _table$outRef$current.scrollLeftAndTop({
                top: 1
              }, function (_ref4) {
                var _table$outRef$current2;
                var top = _ref4.top;
                top && ((_table$outRef$current2 = table.outRef.current) === null || _table$outRef$current2 === void 0 ? void 0 : _table$outRef$current2.scrollTo({
                  scrollTop: 0
                }));
                _ref.current.raf && exec();
              });
            });
          });
        }, function () {
          clear();
          _ref.current.iT = setTimeout(exec, 1000);
        }],
        clear = _ref3[0],
        exec = _ref3[1],
        delayExec = _ref3[2];
      delayExec();
      container.addEventListener('mouseenter', clear);
      container.addEventListener('mouseleave', delayExec);
      return function () {
        clear();
        container.addEventListener('mouseenter', clear);
        container.addEventListener('mouseleave', delayExec);
      };
    }
  }, [scrollbar.vertical, autoScroll]);
}